﻿using ASoft.Base;
using ASoft.Led;
using ASoft.Msg;
using ASoft.Repository;
using ASoft.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASoft.WPF.Base
{
    public class LedTask
    {
        LedService ledService;
        List<byte> roomIds;
        List<byte> phecIds;

        RoomInfoRepository roomInfoRepository = new RoomInfoRepository();
        WeatherStationRepository weatherStationRepository = new WeatherStationRepository();

        public LedTask(LedService ledService)
        {
            this.ledService = ledService;

            var runRooms = RC.Current.RootData.温室集合.Where(p => !RC.Current.RootData.IgnoreRoomIds.Contains(p.Id));
            roomIds = runRooms.Where(p => !p.IsPhec).OrderBy(p => p.Id).Select(p => p.Id).ToList();
            phecIds = runRooms.Where(p => p.IsPhec).OrderBy(p => p.Id).Select(p => p.Id).ToList();
        }

        public RealTimeItem GetRealTimeItem()
        {
            int switchSecond = ledService.LedDriveCfg.SwitchSecond < 3 ? 3 : ledService.LedDriveCfg.SwitchSecond;
            return new RealTimeItem(DateTime.Now.AddSeconds(switchSecond), Run, "Led Task");
        }

        void Run()
        {
            try
            {
                if (ledService.LedDriveCfg.Enabled)
                {
                    //if (!ledService.IsStart)
                    //{
                    //    ledService.Start();
                    //}
                    var currentRIds = roomIds.Skip(Index * 3).Take(3).ToList();
                    if (currentRIds.Count == 0)
                    {
                        ProcessPage2();
                        Index = 0;
                    }
                    else
                    {
                        PorcessPage1(currentRIds);
                        Index++;
                    }
                }
            }
            finally
            {
                MyTaskService.Current.AddItem(GetRealTimeItem());
            }
        }

        private void PorcessPage1(List<byte> currentRIds)
        {
            var datas = currentRIds.Select(p =>
            {
                //var roomInfo = roomInfoRepository.GetLast(p);
                RoomReturnSearchMsg roomInfo = null;
                if (RC.Current.RoomLastMsgs.ContainsKey(p))
                {
                    roomInfo = RC.Current.RoomLastMsgs[p] as RoomReturnSearchMsg;
                }
                if (roomInfo == null)
                {
                    return new RData(p);
                }
                return new RData(p, roomInfo.室内温度1, roomInfo.室内温度2, roomInfo.湿度1, roomInfo.湿度2, roomInfo.室内光照, roomInfo.土壤湿度, roomInfo.室内CO2);
            }).ToList();

            var wData = GetWeatherData();

            var r1 = datas.FirstOrDefault();
            var r2 = datas.Skip(1).FirstOrDefault();
            var r3 = datas.Skip(2).FirstOrDefault();
            if (r2 == null)
                r2 = new RData("");
            if (r3 == null)
                r3 = new RData("");
            ledService.SendPage1(wData, r1, r2, r3);
        }

        private WeatherData GetWeatherData()
        {
            var wInfo = weatherStationRepository.GetLast();
            var wData = new WeatherData();
            if (wInfo != null)
            {
                wData = new WeatherData(wInfo.室外温度,
                    wInfo.光照,
                    wInfo.风速,
                    AppUtility.Get风向字符串((byte)wInfo.风向),
                    AppUtility.Get雨雪字符串((byte)wInfo.雨雪));
            }
            else if (RC.Current.RootData.启用模似气象站)
            {
                wData = new WeatherData(RC.Current.RootData.气象站模似信息.室外温度,
                   RC.Current.RootData.气象站模似信息.光照,
                   RC.Current.RootData.气象站模似信息.风速,
                   AppUtility.Get风向字符串(RC.Current.RootData.气象站模似信息.风向),
                   AppUtility.Get雨雪字符串(RC.Current.RootData.气象站模似信息.雨雪));
            }
            return wData;
        }

        private void ProcessPage2()
        {
            var datas = phecIds.Take(7).Select(p =>
            {
                PhecReturnSearch roomInfo = null;
                if (RC.Current.RoomLastMsgs.ContainsKey(p))
                {
                    roomInfo = RC.Current.RoomLastMsgs[p] as PhecReturnSearch;
                }
                if (roomInfo == null)
                {
                    return new PhecData(p.ToString());
                }
                return new PhecData(RC.Current.RootData.温室集合.First(p1 => p1.Id == p).RoomName,
                    roomInfo.Ph,
                    roomInfo.Ec);
            }).ToList();
            var wData = GetWeatherData();
            ledService.SendPage2(wData, datas);
        }

        public int Index { get; private set; }
    }
}
